package me.zk.practice1.s;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangkai
 * @date 2023/7/13 13:55
 */
public class LS_Offer_57 {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],i);
        }
        for (int i = 0; i < nums.length; i++) {
            int value = target-nums[i];
            if (map.containsKey(value)) {
               return new int[]{nums[i],value};
            }
        }
        return null;
    }
    public int[] twoSum2(int[] nums, int target) {
        for (int i = nums.length-1; i >=0; i--) {
            if (nums[i] < target) {
                int value = target - nums[i];
                for (int j = 0; j < i; j++) {
                    if (nums[j] == value) {
                        return new int[]{nums[j],nums[i]};
                    }
                }
            }
        }
        return null;
    }
    public int[] twoSum3(int[] nums, int target) {
        int i = 0;
        int j = nums.length-1;
        while (i < j) {
            if (nums[i] + nums[j] > target) {
                j--;
            }
            if (nums[i] + nums[j] < target) {
                i++;
            }
            if (nums[i] + nums[j] == target) {
                return new int[]{nums[i],nums[j]};
            }
        }

        return null;
    }
    public static void main(String[] args) {
        new LS_Offer_57().twoSum2(new int[]{12,12,18,37,51,54},30);
    }
}
